<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>编辑角色</title>

    <link rel="stylesheet" href="/static/layui/css/layui.css">

</head>

<body style="padding: 10px;">
    <form class="layui-form">
        <input type="hidden" name="id" value="{$role.id}">

        <!-- 角色名称 -->
        <div class="layui-form-item">
            <label class="layui-form-label">角色名称</label>
            <div class="layui-input-inline">
                <input type="text" name="name" required lay-verify="required" placeholder="请输入角色名称" autocomplete="off"
                    class="layui-input" value="{$role.name}">
            </div>
        </div>

        <!-- 权限树 -->
        <div class="layui-form-item">
            <label class="layui-form-label">权限</label>
            <div class="layui-input-inline">
                <div id="menus-tree"></div>
            </div>
        </div>


        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn" lay-submit lay-filter="formSubmit">提交</button>
            </div>
        </div>
    </form>


    <script src="/static/layui/layui.js"></script>
    <script>
        layui.use(['layer', 'form', 'tree'], function () {
            var layer = layui.layer;
            var $ = layui.jquery;
            var form = layui.form;
            var tree = layui.tree;


            //渲染tree
            var menusTree = tree.render({
                elem: '#menus-tree',   //绑定元素
                id: 'menusTreeId', //定义索引
                showCheckbox: true, //多选框
                accordion: true, //手风琴效果
                data: <?php echo $menus;?>
            });


        //监听提交
        form.on('submit(formSubmit)', function (data) {
            //获得选中的节点
            var checkedMenu = tree.getChecked('menusTreeId');
            $.ajax({
                url: '/admin/role/edit',
                type: 'put',
                async: true,
                dataType: 'json',
                data: { id: data.field.id, name: data.field.name, powers: checkedMenu },
                success: function (res) {
                    if (res.code == '200') {
                        layer.msg('操作成功', {
                            icon: 6,
                            time: 500
                        }, function () {
                            parent.window.location.reload();
                        });
                    } else {
                        layer.msg(res.msg, {
                            icon: 5,
                            time: 1000
                        });
                    }
                }
            });

            return false;
        });
        });

    </script>
</body>

</html>